package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.ejia.dao.DBconnection;
import com.ejia.dao.BaseDao;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.List;
import java.awt.Window;

import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.JToggleButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Toolkit;
import java.awt.Color;

public class login extends JFrame {

	private JPanel contentPane;
	private JTextField txtName;
	private JPasswordField txtPwd;
	public static String cname="admin";
	public static String sf="管理员";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JFrame.setDefaultLookAndFeelDecorated(true);
			UIManager.setLookAndFeel("com.jtattoo.plaf.fast.FastLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					login frame = new login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public login() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(login.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setFont(new Font("Dialog", Font.PLAIN, 30));
		setTitle("E家物业管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1014, 572);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lble = new JLabel("E家物业管理系统");
		lble.setForeground(new Color(18, 150, 219));
		lble.setHorizontalAlignment(SwingConstants.CENTER);
		lble.setFont(new Font("宋体", Font.BOLD, 42));
		lble.setBounds(459, 44, 518, 61);
		contentPane.add(lble);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setFont(new Font("宋体", Font.PLAIN, 26));
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"管理员", "用户","员工"}));
		comboBox.setBounds(645, 352, 269, 44);
		contentPane.add(comboBox);
		
		
		
		
		
		txtName = new JTextField();
		txtName.setFont(new Font("宋体", Font.PLAIN, 30));
		txtName.setBounds(645, 156, 269, 41);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		txtPwd = new JPasswordField();
		txtPwd.setFont(new Font("宋体", Font.PLAIN, 30));
		txtPwd.setBounds(645, 249, 269, 41);
		contentPane.add(txtPwd);
		
		
		
		JButton btnNewButton = new JButton("登录\r\n");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name=txtName.getText();
				cname=name;
				char[] passwrod=txtPwd.getPassword();
				String pwd=new String(passwrod);
				sf=(String) comboBox.getSelectedItem();
				if (name.equals("")||pwd.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入账号密码");
				}else {
					DBconnection.getconn();
					String sql=null;
					if (comboBox.getSelectedIndex()==0) {
						 sql="select * from tb_admin where adminname=? and adminpwd=? and remark=\"超级管理员\"";
						 try {
								if (BaseDao.query(sql,name,pwd).next()) {
									new Main().setVisible(true);
									dispose();
								}else {
									JOptionPane.showMessageDialog(null, "登录名或密码错误，请重新登录!"); 
								}
							} catch (HeadlessException | SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					}else if (comboBox.getSelectedIndex()==1) {
						 sql="select * from tb_user where uid=? and pwd=?";
						 try {
								if (BaseDao.query(sql, name,pwd).next()) {
									new Mainuser().setVisible(true);
									dispose();
								}else {
									JOptionPane.showMessageDialog(null, "登录名或密码错误，请重新登录!"); 
								}
							} catch (HeadlessException | SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					}else {
						 sql="select * from tb_admin where adminname=? and adminpwd=?";
						 try {
								if (BaseDao.query(sql,name,pwd).next()) {
									new Main().setVisible(true);
									dispose();
								}else {
									JOptionPane.showMessageDialog(null, "登录名或密码错误，请重新登录!"); 
								}
							} catch (HeadlessException | SQLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
					}
				}
			}
		});
		
		JLabel label_1 = new JLabel("");
		label_1.setIcon(new ImageIcon(login.class.getResource("/com/ejia/photo/icon_account_add.png")));
		label_1.setFont(new Font("宋体", Font.PLAIN, 30));
		label_1.setBounds(537, 342, 64, 64);
		contentPane.add(label_1);
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 24));
		btnNewButton.setBounds(537, 433, 120, 60);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("注册");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				if (comboBox.getSelectedIndex()==0||comboBox.getSelectedIndex()==2) {
					JOptionPane.showMessageDialog(null, "请联系管理员添加!");
				}else if (comboBox.getSelectedIndex()==1) {
					new zc().setVisible(true);
				}
			
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 24));
		button.setBounds(804, 433, 120, 60);
		contentPane.add(button);
		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(login.class.getResource("/com/ejia/photo/登录.png")));
		label.setBounds(29, 54, 454, 439);
		contentPane.add(label);
		
		JLabel label_2 = new JLabel("");
		label_2.setForeground(UIManager.getColor("TextField.selectionBackground"));
		label_2.setIcon(new ImageIcon(login.class.getResource("/com/ejia/photo/账号.png")));
		label_2.setBounds(537, 141, 64, 70);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("");
		label_3.setIcon(new ImageIcon(login.class.getResource("/com/ejia/photo/密码1.png")));
		label_3.setBounds(537, 234, 64, 70);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("");
		label_4.setIcon(new ImageIcon(login.class.getResource("/com/ejia/photo/未标题-1.png")));
		label_4.setBounds(0, 0, 1000, 552);
		contentPane.add(label_4);
		
		
		setLocationRelativeTo(null);
	}
}
